package com.example.zhu95.mytimber.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.activity.MainActivity;
import com.example.zhu95.mytimber.entity.AlbumBean;
import com.example.zhu95.mytimber.listener.AlbumClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhu95
 * @date 2018/3/6 10:05
 */
public class AlbumSmallAdapter extends RecyclerView.Adapter<AlbumSmallAdapter.ViewHolder> {
    private List<AlbumBean> albums;

    public AlbumSmallAdapter(List<AlbumBean> albumBeans) {
        albums = albumBeans;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_album_small, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final AlbumBean album = albums.get(position);
        holder.tvAlbumSmall.setText(album.getAlbum());
        holder.tvAlbumSmallSongnum.setText("" + album.getSongNum() + "首歌曲");
        String albumArt = album.getAlbumArt();
        if (!TextUtils.isEmpty(albumArt)) {
            Glide.with(holder.itemView.getContext()).load(albumArt).centerCrop().into(holder.imgAlbumSmall);
        } else {
            Glide.with(holder.itemView.getContext()).load(R.drawable.ic_empty_music2).centerCrop().into(holder.imgAlbumSmall);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = holder.itemView.getContext();
                if (context instanceof MainActivity) {
                    MainActivity mainActivity = (MainActivity) context;
                    AlbumClickListener albumClickListener = mainActivity.getAlbumClickListener();
                    albumClickListener.clickAlbum(album.getId());
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return albums.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_album_small)
        ImageView imgAlbumSmall;
        @BindView(R.id.tv_album_small)
        TextView tvAlbumSmall;
        @BindView(R.id.tv_album_small_songnum)
        TextView tvAlbumSmallSongnum;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
